/****************************************************************/
/*																*/
/*  XmlToolBarView.cpp											*/
/*																*/
/*  Implementation of the CXmlToolBarView.cpp class.			*/
/*	The class is the View class of the project					*/											
/*	Purpose of this class is to view the toolbar buttons.		*/																	
/*																*/
/****************************************************************/

#include "stdafx.h"
#include "XmlToolBarView.h"


// CXmlToolBarView

//Enables objects of CObject-derived classes to be created dynamically at run time
IMPLEMENT_DYNCREATE(CXmlToolBarView, CView)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CXmlToolBarView::CXmlToolBarView()
{
}

CXmlToolBarView::~CXmlToolBarView()
{

}

BEGIN_MESSAGE_MAP(CXmlToolBarView, CView)
END_MESSAGE_MAP()


// CXmlToolBarView drawing

void CXmlToolBarView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

// CXmlToolBarView painting bitmap from resource file into dest dc

void CXmlToolBarView::OnPaint(CDC *dc, CDC *dcMem, UINT nIDResource,int x, int y)
{
	//bitmap source initialization to paste it to destination
	CBitmap *bmpSrc = new CBitmap;
	CBitmap *bmpSrcOld = new CBitmap;
	CDC dcMemSrc;					//bitmap source dc memory					

	bmpSrc->LoadBitmap(nIDResource);

	dcMemSrc.CreateCompatibleDC(dc);
	bmpSrcOld = dcMemSrc.SelectObject(bmpSrc);
	
//	bit blit the source to dest dc but image contains transparent area
//	dcMem->BitBlt(x, y, 16, 16, &dcMemSrc, 0, 0, SRCCOPY);

	// transparent blit to the dest. as the megenda is the transparent color
	dcMem->TransparentBlt(x,y,16,16,&dcMemSrc,0,0,16,16,RGB(255,0,255));

	
	//setting the dc memory to it's previous status
	dcMemSrc.SelectObject(bmpSrcOld);
	dcMemSrc.DeleteDC();
	bmpSrcOld->DeleteObject();

}

// CXmlToolBarView painting bitmap from file into dest dc

void CXmlToolBarView::OnPaint(CDC *dc, CDC *dcMem, LPCTSTR szFilename, int x, int y)
{
	CBitmap *bmpSrc = new CBitmap;
	CBitmap *bmpSrcOld = new CBitmap;
	CDC dcMemSrc;

	//load bitmap from file
	!this->LoadBmp(bmpSrc, szFilename);

	dcMemSrc.CreateCompatibleDC(dc);
	bmpSrcOld = dcMemSrc.SelectObject(bmpSrc);

	// transparent blit to the dest. as the megenda has been the transparent color
	dcMem->TransparentBlt(x,y,16,16,&dcMemSrc,0,0,16,16,RGB(255,0,255));

	//holds previous state of the dc
	dcMemSrc.SelectObject(bmpSrcOld);
	dcMemSrc.DeleteDC();
	
	//if image not loaded successfully then follwing lines prevent some illeagal operation
	if(bmpSrcOld)
		bmpSrcOld->DeleteObject();

}

// CXmlToolBarView painting bitmap from file into dest dc

BOOL CXmlToolBarView::LoadBmp(CBitmap* pBmp, LPCTSTR szFilename)
{	
	// bitmap loading from the file in given path
	HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),
											szFilename, IMAGE_BITMAP, 0, 0, 
											LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_DEFAULTCOLOR);
											
    // check for valid handle for bitmap
    if(hBitmap)
    {
		// Delete the current bitmap
		if (pBmp->DeleteObject())

			// If there was a bitmap, that would be detached
			pBmp->Detach(); 
		
		// Attach the currently loaded bitmap to the bitmap object
		pBmp->Attach(hBitmap);
	 
		return true;
    }
	//checks if the image was in the right directory specified in xml file
	//exit if not found
	else
	{
		::AfxMessageBox(_T("Please specify image folder path in xml fie.\n\nToolbar button image file not found."), MB_OKCANCEL, 0);
		exit(0);	
		return false;	
	}
	
}


// CXmlToolBarView diagnostics

#ifdef _DEBUG
void CXmlToolBarView::AssertValid() const
{
	CView::AssertValid();
}

#ifndef _WIN32_WCE
void CXmlToolBarView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif
#endif //_DEBUG


// CXmlToolBarView message handlers
